﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MiruPublic
{
    public class Website
    {
        private WebClient client = new WebClient();
        /// <summary>
        /// Thong tin ve content
        /// </summary>
        private string _Content;
        public string Content
        {
            set 
            { 
                _Content = value;
                WebsiteContentChangedEventArgs args = new WebsiteContentChangedEventArgs();
                args.Content = _Content;
                OnContentChanged(args);
            }
            get {return _Content;}
        }
        public event EventHandler<WebsiteContentChangedEventArgs> ContentChanged;
        protected virtual void OnContentChanged(WebsiteContentChangedEventArgs e)
        {
            EventHandler<WebsiteContentChangedEventArgs> handler = ContentChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler<DownloadStringCompletedEventArgs> DownLoadCompleted;
        protected virtual void OnGetContentSucess(DownloadStringCompletedEventArgs e)
        {
            EventHandler<DownloadStringCompletedEventArgs> handler = DownLoadCompleted;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler<DownloadProgressChangedEventArgs> DownloadProcessChanged;

        protected virtual void OnDownloadProcessChange(DownloadProgressChangedEventArgs e)
        {
            EventHandler<DownloadProgressChangedEventArgs> handler = DownloadProcessChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        public void DownLoadURL(string Url)
        {
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(OnDownloadStringCompleted);
            client.DownloadStringAsync(new Uri(Url));
        }
        private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            OnDownloadProcessChange(e);
        }
        private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            OnGetContentSucess(e);

            if (e.Error == null && !e.Cancelled)
            {
               
            }
        }
    }
    public class WebsiteContentChangedEventArgs : EventArgs
    {
        public string Content{ get; set; }
    }
}
